<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="科目编码" prop="subjectCode">
        <el-input
          v-model="queryParams.subjectCode"
          placeholder="请输入科目编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目名称" prop="subjectName">
        <el-input
          v-model="queryParams.subjectName"
          placeholder="请输入科目名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="助记码" prop="mnemonicCode">
        <el-input
          v-model="queryParams.mnemonicCode"
          placeholder="请输入助记码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级科目" prop="parentSubject">
        <el-input
          v-model="queryParams.parentSubject"
          placeholder="请输入上级科目"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目类别" prop="subjectCategory">
        <el-select v-model="queryParams.subjectCategory" placeholder="请选择科目类别" clearable>
          <el-option
            v-for="dict in subject_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="余额方向" prop="balanceDirection">
        <el-select v-model="queryParams.balanceDirection" placeholder="请选择余额方向" clearable>
          <el-option
            v-for="dict in balance_direction"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="科目状态" prop="subjectStatus">
        <el-select v-model="queryParams.subjectStatus" placeholder="请选择科目状态" clearable>
          <el-option
            v-for="dict in subject_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['subject:accounting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="accountingList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="科目编码" align="center" prop="subjectCode" />
      <el-table-column label="科目名称" align="center" prop="subjectName" />
      <el-table-column label="助记码" align="center" prop="mnemonicCode" />
      <el-table-column label="上级科目" align="center" prop="parentSubject" />
      <el-table-column label="科目类别" align="center" prop="subjectCategory">
        <template #default="scope">
          <dict-tag :options="subject_category" :value="scope.row.subjectCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="余额方向" align="center" prop="balanceDirection">
        <template #default="scope">
          <dict-tag :options="balance_direction" :value="scope.row.balanceDirection"/>
        </template>
      </el-table-column>
      <el-table-column label="科目状态" align="center" prop="subjectStatus">
        <template #default="scope">
          <dict-tag :options="subject_status" :value="scope.row.subjectStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['subject:accounting:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['subject:accounting:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['subject:accounting:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改科目表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="accountingRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父节点" prop="pid">
          <el-tree-select
            v-model="form.pid"
            :data="accountingOptions"
            :props="{ value: 'id', label: 'subjectName', children: 'children' }"
            value-key="id"
            placeholder="请选择父节点"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="科目编码" prop="subjectCode">
          <el-input v-model="form.subjectCode" placeholder="请输入科目编码" />
        </el-form-item>
        <el-form-item label="科目名称" prop="subjectName">
          <el-input v-model="form.subjectName" placeholder="请输入科目名称" />
        </el-form-item>
        <el-form-item label="助记码" prop="mnemonicCode">
          <el-input v-model="form.mnemonicCode" placeholder="请输入助记码" />
        </el-form-item>
        <el-form-item label="上级科目" prop="parentSubject">
          <el-input v-model="form.parentSubject" placeholder="请输入上级科目" />
        </el-form-item>
        <el-form-item label="科目类别" prop="subjectCategory">
          <el-select v-model="form.subjectCategory" placeholder="请选择科目类别">
            <el-option
              v-for="dict in subject_category"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="余额方向" prop="balanceDirection">
          <el-radio-group v-model="form.balanceDirection">
            <el-radio
              v-for="dict in balance_direction"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="科目状态" prop="subjectStatus">
          <el-radio-group v-model="form.subjectStatus">
            <el-radio
              v-for="dict in subject_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数量核算" prop="quantityAccounting">
          <el-checkbox-group v-model="form.quantityAccounting">
            <el-checkbox
              v-for="dict in aaa"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="辅助核算" prop="auxiliaryAccounting">
          <el-checkbox-group v-model="form.auxiliaryAccounting">
            <el-checkbox
              v-for="dict in true_or_false"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="外币核算" prop="foreignCurrencyAccounting">
          <el-checkbox-group v-model="form.foreignCurrencyAccounting">
            <el-checkbox
              v-for="dict in or"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Accounting">
import { listAccounting, getAccounting, delAccounting, addAccounting, updateAccounting } from "@/api/subject/accounting";

const { proxy } = getCurrentInstance();
const { subject_status, balance_direction, subject_category, true_or_false, aaa, or } = proxy.useDict('subject_status', 'balance_direction', 'subject_category', 'true_or_false', 'aaa', 'or');

const accountingList = ref([]);
const accountingOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    subjectCode: null,
    subjectName: null,
    mnemonicCode: null,
    parentSubject: null,
    subjectCategory: null,
    balanceDirection: null,
    subjectStatus: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询科目表列表 */
function getList() {
  loading.value = true;
  listAccounting(queryParams.value).then(response => {
    accountingList.value = proxy.handleTree(response.data, "id", "pid");
    loading.value = false;
  });
}

/** 查询科目表下拉树结构 */
function getTreeselect() {
  listAccounting().then(response => {
    accountingOptions.value = [];
    const data = { id: 0, subjectName: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "id", "pid");
    accountingOptions.value.push(data);
  });
}
	
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    pid: null,
    subjectCode: null,
    subjectName: null,
    mnemonicCode: null,
    parentSubject: null,
    subjectCategory: null,
    balanceDirection: null,
    subjectStatus: null,
    quantityAccounting: [],
    auxiliaryAccounting: [],
    foreignCurrencyAccounting: []
  };
  proxy.resetForm("accountingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.pid = row.id;
  } else {
    form.value.pid = 0;
  }
  open.value = true;
  title.value = "添加科目表";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.pid = row.id;
  }
  getAccounting(row.id).then(response => {
    form.value = response.data;
    form.value.quantityAccounting = form.value.quantityAccounting.split(",");
    form.value.auxiliaryAccounting = form.value.auxiliaryAccounting.split(",");
    form.value.foreignCurrencyAccounting = form.value.foreignCurrencyAccounting.split(",");
    open.value = true;
    title.value = "修改科目表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["accountingRef"].validate(valid => {
    if (valid) {
      form.value.quantityAccounting = form.value.quantityAccounting.join(",");
      form.value.auxiliaryAccounting = form.value.auxiliaryAccounting.join(",");
      form.value.foreignCurrencyAccounting = form.value.foreignCurrencyAccounting.join(",");
      if (form.value.id != null) {
        updateAccounting(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAccounting(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除科目表编号为"' + row.id + '"的数据项？').then(function() {
    return delAccounting(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
